package chatServer;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Iterator;

import IChat.IChatServer;
import IChat.IChatter;


public class ChatServerImpl extends UnicastRemoteObject implements IChatServer{
	
	
	static ChatServerImpl server = null;
	private final static String BINDNAME = "ChatServer";
	private final static String[] STATEMSG = new String[] { "服务器启动", "服务器停止" };
	ArrayList<ChatterInfo> chatters = new ArrayList<ChatterInfo>();
	ArrayList listeners = new ArrayList();
	
	protected ChatServerImpl() throws RemoteException {
		
	}
	//服务器只需一个，因此考虑使用单例模式
	public static ChatServerImpl getInstance() {
		try {
			if (server == null) {
				server = new ChatServerImpl();
			}
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
		return server;
	}
	
	public void start() throws RemoteException, MalformedURLException {
		java.rmi.Naming.rebind(BINDNAME, server);
//		notifyListener(STATEMSG[0]);
	}
	
	public void stop() throws RemoteException, NotBoundException, MalformedURLException {
//		notifyListener(STATEMSG[1]);
		Iterator itr = chatters.iterator();
		while (itr.hasNext()) {
			ChatterInfo u = (ChatterInfo) itr.next();
			u.getChatter().serverStop();
		}
		Naming.unbind(BINDNAME);
	}
	
	@Override
	public void login(String name, IChatter chatter) throws RemoteException {
		if (chatter != null && name != null) {
			ChatterInfo newChatterInfo = new ChatterInfo(name, chatter);
//			notifyListener(u.getName() + " 进入聊天室");
			Iterator itr = chatters.iterator();
			while (itr.hasNext()) {
				ChatterInfo oldChatterInfo = (ChatterInfo) itr.next();
				//通知其他用户有新用户进入聊天室，并且将新用户加进各用户的hash中
				oldChatterInfo.getChatter().receiveEnter(name, chatter, false);
				//将其他在线用户加进新用户的hash中，hasEntered用于控制新用户不需要接收“在线用户进入聊天室的信息”
				chatter.receiveEnter(oldChatterInfo.getName(), oldChatterInfo.getChatter(), true);
			}
			chatters.add(newChatterInfo);
		}
	}
	@Override
	public void logout(String name) throws RemoteException {
		if (name == null) {
			System.out.println("用户名为空!");
			return;
		}
		ChatterInfo u_gone = null;
		Iterator itr = null;
		
		synchronized (chatters) {
			for (int i = 0; i < chatters.size(); i++) {
				ChatterInfo u = (ChatterInfo) chatters.get(i);
				if (u.getName().equals(name)) {
//					notifyListener(name + " 离开聊天室");
					u_gone = u;
					chatters.remove(i);
					itr = chatters.iterator();
					break;
				}
			}
		}
		
		if (u_gone == null || itr == null) {
			System.out.println("找不到用户" + name + "，无法登出");
			return;
		}
		
		while (itr.hasNext()) {
			ChatterInfo u = (ChatterInfo) itr.next();
			u.getChatter().receiveExit(name);
		}
	}
	//chat目前只用于通知全体在线用户
	@Override
	public void chat(String name, String message) throws RemoteException {
		Iterator itr = chatters.iterator();
		
		while (itr.hasNext()) {
			ChatterInfo u = (ChatterInfo) itr.next();
			if (!name.equals(u.getName()))
				u.getChatter().receiveChat(name, message);
		}
//		notifyListener(name + ":" + message);
	}
	
	
	
}
